package cn.sunlight.college.serviceEdu.service.impl;

import cn.sunlight.college.serviceEdu.entity.EduSubject;
import cn.sunlight.college.serviceEdu.entity.excel.SubjectData;
import cn.sunlight.college.serviceEdu.entity.subject.OneSubject;
import cn.sunlight.college.serviceEdu.entity.subject.TwoSubject;
import cn.sunlight.college.serviceEdu.listener.SubjectExcelListener;
import cn.sunlight.college.serviceEdu.mapper.EduSubjectMapper;
import cn.sunlight.college.serviceEdu.service.EduSubjectService;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author sunlight
 * @since 2021-06-14
 */
@Slf4j
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {
        try {
            InputStream in = file.getInputStream();
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
            log.debug("读取失败:{}",e.getMessage());
        }
    }

    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //1.查询所有的一级分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);

        //2.查询所有的二级分类
        QueryWrapper<EduSubject> wrapperTow = new QueryWrapper<>();
        wrapperTow.ne("parent_id","0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTow);

        //创建list集合，用于存储最终封装数据
        List<OneSubject> finalSubjectList=new ArrayList<>();

        //3.封装一级分类
        //查询出来所有的一级分类的list集合遍历，得到每一个一级分类对象，获取每个一级分类对象值，封装到要求的list集合里面
        for (int i = 0; i < oneSubjectList.size(); i++) {//遍历oneSubjectList集合
            //得到oneSubjectList每个eduSubject对象
            EduSubject eduSubject = oneSubjectList.get(i);

            //把eduSubject里面值获取出来，放到oneSubjectList对象里面，然后再把oneSubjectList放到finalSubjectListlimian
            OneSubject oneSubject = new OneSubject();
            /*oneSubject.setId(eduSubject.getId());
            oneSubject.setTitle(eduSubject.getTitle());*/
            BeanUtils.copyProperties(eduSubject,oneSubject);

            finalSubjectList.add(oneSubject);
            //在一级分类循环遍历查询所有的二级分类
            List<TwoSubject> twoFinalSubjectList=new ArrayList<>();
            //遍历二级分类
            for (int m = 0; m < twoSubjectList.size(); m++) {
                //获取每个二级分类
                EduSubject tSubject = twoSubjectList.get(m);
                //判断二级分类parant_id和一级分类ID是否一样
                if (tSubject.getParentId().equals(eduSubject.getId())){
                    //把tSubject值复制到TwoSubject里面，放到twoFinalSubjectList里面
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject,twoSubject);
                    twoFinalSubjectList.add(twoSubject);
                }
            }

            //把一级下面的所有二级分类放进去
            oneSubject.setChildren(twoFinalSubjectList);
        }

        return finalSubjectList;
    }
}
